package br.com.venda.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import br.com.exception.DadosInvalidosException;
import br.com.item.Item;
import br.com.item.ItemFaixada;
import br.com.negocio.VendaService;
import br.com.pessoa.Pessoa;
import br.com.venda.ItemVenda;
import br.com.venda.Venda;
import br.com.venda.VendaFaixada;

public class VendaFinalizar extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public VendaFinalizar() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		HttpSession sessao = request.getSession();
		
								
		ArrayList<ItemVenda> itensVenda = (ArrayList<ItemVenda>) sessao.getAttribute("itensvenda");
		Pessoa cliente = (Pessoa) sessao.getAttribute("cliente");
		Pessoa funcionario = (Pessoa) sessao.getAttribute("funcionario");
		
		String situacao = "pago";
		
		Venda venda = null;
		try {
			venda = new VendaFaixada().criarVenda(situacao, funcionario, cliente, itensVenda);
		} catch (DadosInvalidosException e) {
			// TODO Auto-generated catch block
			System.out.println(e);
		}
		
		new VendaFaixada().incluirVenda(venda);
		
		//request.setAttribute("total", total);

		sessao.removeAttribute("funcionario");
		sessao.removeAttribute("itensvenda");
		sessao.removeAttribute("cliente");
		
		
		sessao.setAttribute("itensvenda", itensVenda);
			
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("IniciarVenda");
		
		
		dispatcher.forward(request,response);
		
	}
}
